package com.lk.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lk.entity.Material;
import com.lk.util.DBHelp;
import com.lk.util.RowMapper;

public class MaterialDao {
	
	DBHelp<Material> db = new DBHelp<Material>();
	
	public void save(Material m){
		String sql = "insert into t_material(id,m_title,m_content,m_createTime,t_project_id,t_employee_id) values(?,?,?,?,?,?)";
		db.executeSQL(sql, m.getM_id(),m.getM_title(),m.getM_content(),m.getM_createTime(),m.getT_project_id(),m.getT_employee_id());
	}
	
	public void delByMaterialId(String m_id){
		String sql = "delete from t_material where id = ?";
		db.executeSQL(sql, m_id);
	}
	
	public void update(Material m){
		String sql = "update t_material set m_title = ? , m_content = ? where id = ?";
		db.executeSQL(sql, m.getM_title(),m.getM_content(),m.getM_id());
	}
	
	public Material findById(String t_project_id,String t_employee_id){
		String sql = "select id,m_title,m_content,m_createTime,t_project_id,t_employee_id from t_material where t_project_id = ? and t_employee_id = ?";
		return db.executeSQLForObject(sql, new MaterialRowMapper(), t_project_id,t_employee_id);
	}
	
	public Material findByMaterialId(String m_id){
		String sql = "select id,m_title,m_content,m_createTime,t_project_id,t_employee_id from t_material where id = ?";
		return db.executeSQLForObject(sql, new MaterialRowMapper(), m_id);
	}
	
	public List<Material> findAll(){
		String sql = "select id,m_title,m_content,m_createTime,t_project_id,t_employee_id from t_material";
		return db.executeSQLForList(sql, new MaterialRowMapper());
	}
	
	public class MaterialRowMapper implements RowMapper<Material>{
		public Material rowmapper(ResultSet rs) throws SQLException {
			Material m = new Material();
			m.setM_id(rs.getString("id"));
			m.setM_title(rs.getString("m_title"));
			m.setM_content(rs.getString("m_content"));
			m.setM_createTime(rs.getString("m_createTime"));
			m.setT_project_id(rs.getString("t_project_id"));
			m.setT_employee_id(rs.getString("t_employee_id"));
			return m;
		}
		
	}
}
